package fr.afcepf.ai89.esharing.data;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import fr.afcepf.ai89.esharing.entity.FrDepartement;
import fr.afcepf.ai89.esharing.entity.FrRegion;

@Stateless
public class DAOFrDepartement implements IDAOFrDepartement {

	@PersistenceContext(unitName = "ESharingPhase22")
	EntityManager em;

	@Override
	public FrDepartement getDept(String idDept) {
		
		Query hql = em.createQuery("SELECT d from FrDepartement d WHERE d.idDepartement = :param");

		hql.setParameter("param", idDept);
		List<FrDepartement> depts = (List<FrDepartement>)hql.getResultList();
		
		FrDepartement dept = null;
		
		if (!depts.isEmpty()) {
			
			dept = (FrDepartement)depts.get(0);
			
		}

		return dept;
		
	}

	@Override
	public List<FrDepartement> getDepts() {
		
		Query hql = em.createQuery("SELECT d from FrDepartement d");

		List<FrDepartement> depts = (List<FrDepartement>)hql.getResultList();

		return depts;
		
	}

	@Override
	public List<FrDepartement> getDepts(FrRegion region) {
		
		Query hql = em.createQuery("SELECT d from FrDepartement d WHERE d.frRegion.idRegion = :param");

		hql.setParameter("param", region.getIdRegion());
		List<FrDepartement> depts = (List<FrDepartement>)hql.getResultList();

		return depts;
		
	}

	@Override
	public int addDept(FrDepartement dept) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int majDept(FrDepartement dept) {
		// TODO Auto-generated method stub
		return 0;
	}

}
